/*
 *
 *  Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Raúl Román López <rroman@gsyc.es>
 *
 */

package com.LibreGeoSocial.App.AR.Overlays;

import java.util.ArrayList;

import com.LibreGeoSocial.App.R;
import com.LibreGeoSocial.App.AR.ARGeoNode;
import com.LibreGeoSocial.App.AR.ARUtils;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.os.Handler;
import android.os.Message;
import android.view.View;

public class DrawRadar extends View{

	private static final float mRADIUS = 55;
	private static final float mRADIUSANGLE = 50;
	private static final float maxRadiusResource = 40;
	private static final float resourceRadius = 2;
	
	private Context mContext;
	private float azimuth;
	private ArrayList<Point> resources= null;
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			invalidate();
		}
	};
	
	public DrawRadar(Context context){
		super(context);
		
		this.mContext = context;
		azimuth = 0;
	}
	
	public void setResourcesList(final ArrayList<ARGeoNode> nodes){
		new Thread(){
			public void run(){
				int max = nodes.size();
				
				ArrayList<Point> points = new ArrayList<Point>();
	        	for(int i = 0; i < max; i++){
	        		points.add(nodes.get(i).getPoint());
	        	}	
	        	resources = points;
	        	
	        	mHandler.sendEmptyMessage(0);
			}
		}.start();
	}

	public void setAzimuth(float azimuth){
		this.azimuth = azimuth;
	}
	
	@Override
	protected void onDraw(Canvas canvas){
		int w = canvas.getWidth();
        int h = canvas.getHeight();
        float cx = ((float)w) / 2 ;
        float cy = ((float)h) / 2 - 12.5f ;
		
        float compass_center_x = ARUtils.transformPixInDip(mContext, 180);
        float compass_center_y = ARUtils.transformPixInDip(mContext, 87.5f);
        
        // VISION RANGE
        Paint paintRange = new Paint();
		paintRange.setStyle(Paint.Style.FILL);
		paintRange.setARGB(250, 245, 255, 245);
		
        RectF oval = new RectF(cx + compass_center_x - mRADIUSANGLE, cy + compass_center_y - mRADIUSANGLE, 
        		cx + compass_center_x + mRADIUSANGLE, cy + compass_center_y + mRADIUSANGLE);
        
        canvas.drawArc(oval, azimuth - 90 - DrawResource.MAX_AZIMUTH_VISIBLE,
        		DrawResource.MAX_AZIMUTH_VISIBLE*2, true, paintRange);
        
        // RADAR BACKGROUND
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.compass_cool), 
        		cx + compass_center_x - mRADIUS, cy + compass_center_y - mRADIUS, null);
        
        // RESOURCE POINTS
        if(resources != null){
        	Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setARGB(255, 255, 255, 255);
            
        	int max = resources.size();
        	for(int i = 0; i < max; i++)
        		canvas.drawCircle(cx + compass_center_x + ((float)maxRadiusResource * resources.get(i).x)/100, 
        				cy + compass_center_y + ((float)maxRadiusResource * resources.get(i).y)/100,
        				resourceRadius, paint);
        }
        
        // CENTER RADAR
        Paint centerPaint = new Paint();
        centerPaint.setStyle(Paint.Style.FILL);
        centerPaint.setShader(new RadialGradient(cx + compass_center_x, cy + compass_center_y, 5, Color.RED, Color.BLACK, TileMode.MIRROR)); 

        RectF center = new RectF(cx + compass_center_x-5, cy + compass_center_y-5, cx + compass_center_x+5, cy + compass_center_y+5);

        canvas.drawOval(center, centerPaint);

		super.onDraw(canvas);
	}
	
}